# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_realname=codelite
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=14.0
pkgrel=2
pkgdesc="Open-source, cross platform IDE for the C/C++ programming languages (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('GPL')
url="https://www.codelite.org/"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-hunspell"
         "${MINGW_PACKAGE_PREFIX}-libssh"
         "${MINGW_PACKAGE_PREFIX}-drmingw"
         "${MINGW_PACKAGE_PREFIX}-clang"      # libclang
         "${MINGW_PACKAGE_PREFIX}-uchardet"
         "${MINGW_PACKAGE_PREFIX}-wxWidgets"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         )
optdepends=("${MINGW_PACKAGE_PREFIX}-clang")
makedepends=("${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             #"${MINGW_PACKAGE_PREFIX}-clang"
             "git")
options=(strip staticlibs !debug)
source=("${_realname}-${pkgver}.tar.gz::https://github.com/eranif/codelite/archive/${pkgver}.tar.gz"
        001-include-winsock-header.patch
        002-mingw-use-wx-manifests.patch
        003-use-gnu-extensions.patch
        004-windows-headers-conflict.patch
        005-fix-sqlite3-find-and-build.patch
        006-proper-libraries-names-mingw.patch
        007-strip-whitespaces-flags.patch
        010-strange-conflicts.patch
        011-use-std_string.patch
        012-define-wxCONTROL_NONE.patch
        013-fix-linking-libraries-names.patch
        014-fix-link-dup-error.patch
        015-remove-WXDLLIMPEXP_SDK-from-codelite_terminal.patch)
sha256sums=('67c7947808552ac07c6c71f8cd53440d293da03d89176b9dfb38012c5dd662b4'
            'f1babfd20f19c417d32392d7200950fec93ea27022ec1e377f7d4b89332df3da'
            'cfac30a871df28c19330f0cc607ac57b64ae54896af22c0e189de4f76c0738b7'
            'fbed169ac80a8679c3b5fadb2521a61745c98eebb65050a088d82240b0da37d5'
            '7b8d62c0af7cce47de53be38f5b4e517b464e19f60785e9757bbe9340608928d'
            '3521cea8184a1a6c3a3dac7de5b94cb6e1ee337962f317cb2e71063f860b90f8'
            '1206b6453000e1dbe8058fb646c97599b2a072f4aad412047f439b288e662777'
            'a9e5539e31c664a84e42412cdd30b44699a92efb2fb6f51000010bb958c10219'
            '5a6312aa0373587cf89e5a025fa81dc4f249a37db103e133a27bb15f4a611d1b'
            'a673c4309fa09e52a1e3917fef3c0b5f95149b1860da1140a81769342ef9352c'
            '9a027cd400daf8310faeaf56111d2e8b6d501a38ae0ec0ae86ce14213137841d'
            'd1f58f6c7afcb13453004d56db32640427e214fe3c96faacbddd270e4f5137ed'
            '73352c5718538f022407cec30b57e5805accd5f32dcabae2b431c68a64c0e418'
            'd4f90a769e060c048cf4fb3fe907271fe22378fcdf0b82ec4969d77c2936045c')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001-include-winsock-header.patch
  patch -p1 -i ${srcdir}/002-mingw-use-wx-manifests.patch
  patch -p1 -i ${srcdir}/003-use-gnu-extensions.patch
  patch -p1 -i ${srcdir}/004-windows-headers-conflict.patch
  patch -p1 -i ${srcdir}/005-fix-sqlite3-find-and-build.patch
  patch -p1 -i ${srcdir}/006-proper-libraries-names-mingw.patch
  patch -p1 -i ${srcdir}/007-strip-whitespaces-flags.patch
  patch -p1 -i ${srcdir}/010-strange-conflicts.patch
  patch -p1 -i ${srcdir}/011-use-std_string.patch
  #patch -p1 -i ${srcdir}/012-define-wxCONTROL_NONE.patch
  patch -p1 -i ${srcdir}/013-fix-linking-libraries-names.patch
  patch -p1 -i ${srcdir}/014-fix-link-dup-error.patch
  patch -p1 -i ${srcdir}/015-remove-WXDLLIMPEXP_SDK-from-codelite_terminal.patch
}

build() {
  local -a extra_config

  if check_option "debug" "y"; then
    extra_config+=( -DCMAKE_BUILD_TYPE=Debug )
  else
    extra_config+=( -DCMAKE_BUILD_TYPE=Release )
  fi

  mkdir -p build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="-DCL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -Wno-dev \
    -G"MSYS Makefiles" \
    -DCL_PREFIX=${MINGW_PREFIX} \
    -DENABLE_CLANG=1 \
    -DWITH_WEBVIEW=1 \
    -DwxWidgets_CONFIG_EXECUTABLE=${MINGW_PREFIX}/bin/wx-config \
    -DwxWidgets_wxrc_EXECUTABLE=${MINGW_PREFIX}/bin/wxrc-3.0 \
    "${extra_config[@]}" \
    ../${_realname}-${pkgver}

  make # -j1 VERBOSE=1
}

package() {
  cd build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  # why?
  rm -f ${pkgdir}${MINGW_PREFIX}/lib/codelite/*.a
  rm -f ${pkgdir}${MINGW_PREFIX}/lib/*.a
}
